package com.guapi.gwt.reference.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.guapi.gwt.api.client.GuardianApiServiceAsync;
import com.guapi.gwt.parser.client.model.Content;

public class ContentApp extends DockPanel{

	private final GuardianApiServiceAsync apiService;

	public ContentApp(GuardianApiServiceAsync apiServiceImpl) {
		this.apiService = apiServiceImpl;
		
		VerticalPanel topPanel = new VerticalPanel();
		this.add(topPanel, NORTH);
		
		topPanel.add(new Label("Example ids:"));
		
		FlowPanel infoPanel = new FlowPanel();
		topPanel.add(infoPanel);
		String info = "Article: 350488002 - Gallery: 350453297 - Interactive: 346581831 - Video: 350473366 - Poll: 350447626 - Audio: 350473169 - Competition: 350450419";
		
		Label infoLabel = new Label(info);
		infoPanel.add(infoLabel);
		
		
		HorizontalPanel inputPanel = new HorizontalPanel();
		inputPanel.setStyleName("inputPanel");
		inputPanel.add(new Label("Enter id to retreive: "));
		final TextBox id = new TextBox();
		id.setValue("344089365");
		Button submit =  new Button("Get content");
		inputPanel.add(id);
		inputPanel.add(submit);
		topPanel.add(inputPanel);
		
		final VerticalPanel resultPanel = new VerticalPanel();
		this.add(resultPanel, CENTER);
		
		final AsyncCallback<Content> callback = new AsyncCallbackImplementation(resultPanel);
		
		ClickHandler submitHandler = new SubmitClickHandler(callback, id);
		submit.addClickHandler(submitHandler);
	}
	
	private final class SubmitClickHandler implements ClickHandler {
		private final AsyncCallback<Content> callback;
		private final TextBox id;

		private SubmitClickHandler(AsyncCallback<Content> callback,
				TextBox id) {
			this.callback = callback;
			this.id = id;
		}

		public void onClick(ClickEvent event) {
			apiService.getContent(Integer.parseInt(id.getText()), callback);
		}
	}

	private final class AsyncCallbackImplementation implements AsyncCallback<Content> {
		
		private final VerticalPanel resultPanel;

		private AsyncCallbackImplementation(VerticalPanel resultPanel) {
			this.resultPanel = resultPanel;
		}

		public void onFailure(Throwable error) {
			final DialogBox errorBox = new DialogBox();
			errorBox.setModal(true);
			errorBox.setAnimationEnabled(true);
			errorBox.setText("Error");
			VerticalPanel panel = new VerticalPanel();
			panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			panel.add(new Label(error.getMessage()));
			Button ok = new Button("Ok");
			ok.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					errorBox.hide();
				}
		
			});
	
			panel.add(ok);
	
			errorBox.add(panel);
			errorBox.center();
		}

		public void onSuccess(Content content) {
			if (resultPanel.getWidgetCount() == 1) {
				resultPanel.remove(0);
			}
			ContentDisplay contentDisplay = new ContentDisplay(content);
			resultPanel.add(contentDisplay);
		}
	}
	
}
